package com.example.springboot005tx.service.impl;

import com.example.springboot005tx.mapper.UserInfoMapper;
import com.example.springboot005tx.mapper.UserMapper;
import com.example.springboot005tx.pojo.User;
import com.example.springboot005tx.pojo.UserInfo;
import com.example.springboot005tx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/6/18 21:30
 * @version: v1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Transactional

    @Override
    public Boolean userInsert(User user, UserInfo userInfo) {
        int count = userMapper.insert(user);
        if (count != 1){
            throw new RuntimeException("User 插入失败");
        }
        // 模拟异常
        String s = null;
        s.toString();

        userInfo.setId(user.getId());
        count += userInfoMapper.insert(userInfo);


        if (count == 1){
            throw new RuntimeException("userInfo 插入失败");
        }

        return count == 2 ? true : false;
    }
}
